{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3608788f-91c6-4caa-a96b-f5f8663a291a",
   "metadata": {},
   "source": [
    "# The `R_functions` notebook  <a id=\"R_fun\"></a>[<font size=1>(back to `Main.ipynb`)</font>](./Main.ipynb)\n",
    "\n",
    "This notebook gathers the functions that requires calling the [R language](https://www.r-project.org/).  \n",
    "This allows these functions to be isolated from the rest of the code. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "718d9976-16bb-4b0a-99e3-e0ae75c2bd1f",
   "metadata": {},
   "source": [
    "The function \n",
    "\n",
    "> `aggregate_labor(economy::Economy)`\n",
    "\n",
    "computes the  persistence and innovation of the aggregate labor process. The computation involves a regression of the simulated aggregate process (with the proper weights). \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "192475c4-a367-4193-8cd7-14b0b405fe12",
   "metadata": {},
   "outputs": [],
   "source": [
    "function aggregate_labor(economy::Economy)\n",
    "    n_type = sum(economy.nys) # number of types\n",
    "    total_n = 100 # total number of simulations\n",
    "    n_simul = 1 .+floor.(Int, economy.Sy*total_n) # vector of numbers of simulations by type\n",
    "    mc = MarkovChain(economy.Πy,log.(economy.ys))\n",
    "    \n",
    "    length_panel = 1000\n",
    "    xv = zeros(Float64,sum(n_simul)*length_panel,4)\n",
    "    n_obs = 1\n",
    "    n_agent = 1\n",
    "    for i=1:n_type\n",
    "        for k=1:n_simul[i] # I simulate n_simul[i] agents of each type i\n",
    "            simul = simulate(mc, length_panel , init = i) # I simulate the process for \n",
    "            for j = 1:length_panel        \n",
    "                xv[n_obs,1] = n_agent # identifyer agent\n",
    "                xv[n_obs,2] = i #type\n",
    "                xv[n_obs,3] = j # date\n",
    "                xv[n_obs,4] = simul[j] #wage\n",
    "                n_obs +=1\n",
    "            end\n",
    "            n_agent +=1\n",
    "        end\n",
    "    end\n",
    "    df = DataFrame(agent =convert.(Int,xv[:,1]) ,type = convert.(Int,xv[:,2]),date=type = xv[:,3],prod=type = xv[:,4]);\n",
    "    ### Computing persistence and innovation of the aggregate labor process    \n",
    "    R\"\"\"\n",
    "    df3 = $df\n",
    "    #save(df3,file = \"mydata.RData\")\n",
    "    library(plm)\n",
    "    library(dplyr)\n",
    "    \n",
    "    #rm(list = ls())\n",
    "    \n",
    "    #load(\"../mydata.RData\")\n",
    "    \n",
    "    panel_data <- pdata.frame(df3, index = c(\"agent\", \"date\"))\n",
    "    panel_data<-transform(panel_data, l_prod=lag(prod,1))\n",
    "    \n",
    "    \n",
    "    panel_data<-panel_data %>% filter(as.integer(date) >1) # I remote the first date\n",
    "    model <-plm(prod ~ l_prod+0, data = panel_data, index = c(\"agent\", \"date\"), model = \"within\")\n",
    "    \n",
    "    result <- panel_data %>%\n",
    "      group_by(agent) %>%\n",
    "      mutate(res = prod -model$coefficients[1]*l_prod)\n",
    "    \n",
    "    coef1 = model$coefficients[1]\n",
    "    sd1 = sd(result$res)\n",
    "    \"\"\"\n",
    "    sd_lab = @rget(sd1)\n",
    "    rho_lab = @rget(coef1)\n",
    "    \n",
    "    return rho_lab,sd_lab\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f477c0cc-ff92-4555-b158-91698c61bc92",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 1.10.5",
   "language": "julia",
   "name": "julia-1.10"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.10.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
